package cn.fotoit.product.system.service;

import cn.fotoit.product.system.entity.UserLog;
import cn.fotoit.product.system.repository.UserLogRepository;
import cn.fotoit.product.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;


@Service
@CacheConfig(cacheNames = "queue_management_userLogService")
@Transactional(readOnly = true)
public class UserLogService {
    @Autowired
    private UserLogRepository userLogRepository;

    @CacheEvict(allEntries = true,cacheNames = {"queue_management_permissionService","queue_management_roleRelPermissionService","queue_management_roleService","queue_management_userService"})
    @Transactional
    public void add(UserLog userLog) {
        userLogRepository.save(userLog);
    }

    @CacheEvict(allEntries = true,cacheNames = {"queue_management_permissionService","queue_management_roleRelPermissionService","queue_management_roleService","queue_management_userService"})
    @Transactional
    public void deleteById(Integer id) {
        userLogRepository.deleteById(id);
    }


    @Cacheable(key = "'findAll'+#page+'size'+#size")
    public Page<UserLog> findAll(Integer page, Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return userLogRepository.findAll(pageable);
    }

    @Cacheable(key = "'findById'+#id")
    public UserLog findById(Integer id) {
        Optional<UserLog> byId = userLogRepository.findById(id);
        if (byId.isPresent()) {
            return byId.get();
        }
        return null;
    }

    @Cacheable(key = "'findByType'+#type+'page'+#page+'size'+#size")
    public Page<UserLog> findByType(Integer type, Integer page, Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return userLogRepository.findByType(type, pageable);
    }

    @Cacheable(key = "'findByUserId'+#userId+'page'+#page+'size'+#size")
    public Page<UserLog> findByUserId(Integer userId, Integer page, Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return userLogRepository.findByUserId(userId, pageable);
    }
    @Cacheable(key = "'findByUserIdAndType'+#userId+'type'+#type+'page'+#page+'size'+#size")
    public Page<UserLog> findByUserIdAndType(Integer userId, Integer type, Integer page, Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return userLogRepository.findByUserIdAndType(userId, type, pageable);
    }
}